import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class CreateXMLFile3 {
	private String[] sduration = new String[12];
	private String[] sstate    = new String[12];
	private int[]    time      = new int[12];
   private int      numSeq;

	//Set up US road style in that you can turn right on red only way to do that is make the 
	//light green, they will break if they see any cars coming.
	
	private String[] state = {"ggrrrrggrrrr",
                             "rrrggrrrrggr",
                             "rrggrrrrggrr",
	                          "grrrrggrrrrg",
                             "rrrrrrggggrr",
                             "ggggrrrrrrrr",
                             "rrrggggrrrrr",
                             "grrrrrrrrggg"};
	
	public CreateXMLFile3(Chromo X){
		numSeq = X.BitToInt(X,0,3);
      numSeq+=4;
		int seqCount = 0;
		for(int i=3;i<(10*numSeq+3);i+=10){
			time[seqCount] = X.BitToInt(X, i, i+7) + 1;
			sduration[seqCount] = Integer.toString(time[seqCount]);
			sstate[seqCount] = state[X.BitToInt(X, i+7, i+10)];
			seqCount++;
		}

		buildXML();

	}


	private void buildXML() {
		
		try{
		 
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 
			//root elements
			Document doc     = docBuilder.newDocument();
			Element add      = doc.createElement("add");
			doc.appendChild(add);

			Element tl_logic = doc.createElement("tl-logic");
			add.appendChild(tl_logic);
			
			Attr attrid = doc.createAttribute("id");
			attrid.setValue("Intersection");
			tl_logic.setAttributeNode(attrid);
			
			Attr attrtype = doc.createAttribute("type");
			attrtype.setValue("static");
			tl_logic.setAttributeNode(attrtype);
			
			Attr attrpgid = doc.createAttribute("programID");
			attrpgid.setValue("1");
			tl_logic.setAttributeNode(attrpgid);
			
			Attr attroffset = doc.createAttribute("offset");
			attroffset.setValue("0");
			tl_logic.setAttributeNode(attroffset);
			
			for(int j=0; j<numSeq; j++){
				Element phase = doc.createElement("phase");
				Attr attrduration = doc.createAttribute("duration");
				//Set This from code
				attrduration.setValue(sduration[j]);
				phase.setAttributeNode(attrduration);
				Attr attrstate = doc.createAttribute("state");
				//Set This from code
				attrstate.setValue(sstate[j]);
				phase.setAttributeNode(attrstate);
				
				tl_logic.appendChild(phase);
			}
			
			//Add line to tell SUMO to collect lane data statistics
			Element meanDataLane = doc.createElement("meandata-lane");
			Attr meanDataLaneId = doc.createAttribute("id");
			meanDataLaneId.setValue("laneStatistics");
			meanDataLane.setAttributeNode(meanDataLaneId);
			Attr meanDataLaneFrequency = doc.createAttribute("freq");
			meanDataLaneFrequency.setValue(Integer.toString(Parameters.meanLaneDataSampleTimeSeconds));
			meanDataLane.setAttributeNode(meanDataLaneFrequency);
			Attr meanDataLaneFile = doc.createAttribute("file");
			

			String output = Parameters.sumoOutputPath + Parameters.sumoLaneStatisticsOutputFile;
			meanDataLaneFile.setValue(output);
         
			meanDataLane.setAttributeNode(meanDataLaneFile);

			add.appendChild(meanDataLane);

			//write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result =  new StreamResult(new File(Parameters.sumoNetworkRoutePath, Parameters.sumoAdditionalParamFile));
			transformer.transform(source, result);

	    }catch(ParserConfigurationException pce){
	    	pce.printStackTrace();
	    }catch(TransformerException tfe){
	    	tfe.printStackTrace();
	    }
	}
	
	public int[] getTime(){
		return time;
	}
}
